package com.primb.bench.base;


import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.flyco.dialog.widget.NormalDialog;
import com.primb.baselibs.view.loading.SpotsDialog;
import com.primb.bench.R;

import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * fragment基类
 */
public abstract class BaseFragment extends Fragment {

    public BaseActivity mActivity;
    private Unbinder unbinder;
    private SpotsDialog loadingDialog;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View rootView = inflater.inflate(onBaseLayoutId(), container, false);
        unbinder = ButterKnife.bind(this, rootView);
        return rootView;
    }

    protected abstract int onBaseLayoutId();

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        onBaseBindView();

        onBaseLoadData();
    }

    protected abstract void onBaseBindView();

    protected abstract void onBaseLoadData();

    @Override
    public void onStart() {
        super.onStart();
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    @Override
    public void onPause() {
        super.onPause();
    }

    @Override
    public void onStop() {
        super.onStop();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (unbinder != null) {
            unbinder.unbind();
        }
    }

    public void showToast(String text) {
        if (text == null || TextUtils.isEmpty(text))
            return;
        Toast.makeText(getContext(), text, Toast.LENGTH_SHORT).show();
    }

    public void showLoadingDialog(String msg) {
        if (loadingDialog == null) {
            loadingDialog = new SpotsDialog(getContext(), msg);
        } else {
            loadingDialog.setMessage(msg);
        }
        loadingDialog.show();
    }

    public void hideLoadingDialog() {
        if (loadingDialog != null && loadingDialog.isShowing()) {
            loadingDialog.dismiss();
        }
    }

    public NormalDialog showAlterDialogTwo(String title, String message) {
        NormalDialog dialog = new NormalDialog(getContext());
        dialog.widthScale(0.8f);
        dialog.style(NormalDialog.STYLE_TWO);
        dialog.titleLineColor(ContextCompat.getColor(getContext(), R.color.app_main_gray));
        dialog.title(title);
        dialog.btnNum(2);
        dialog.content(message);
        dialog.dividerColor(ContextCompat.getColor(getContext(), R.color.app_main_gray));
        dialog.show();
        return dialog;
    }

    public NormalDialog showAlterDialogOne(String title, String message) {
        NormalDialog dialog = new NormalDialog(getContext());
        dialog.widthScale(0.8f);
        dialog.style(NormalDialog.STYLE_TWO);
        dialog.titleLineColor(ContextCompat.getColor(getContext(), R.color.app_main_gray));
        dialog.title(title);
        dialog.btnNum(1);
        dialog.content(message);
        dialog.dividerColor(ContextCompat.getColor(getContext(), R.color.app_main_gray));
        dialog.btnText("确定");
        dialog.show();
        return dialog;
    }

    @Override
    public void onDetach() {
        super.onDetach();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }
}
